using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEngine.UI;

// 用于在地图上随机添加障碍物

public class AddObstacle : MonoBehaviour
{
    // Start is called before the first frame update
    public Button btn;
    public GameObject env;
    public GameObject[] prefab;
    public Tilemap map;
    public int num;
    private bool flag;
    void Start()
    {
        btn.onClick.AddListener(foo);
        flag = false;
    }

    // Update is called once per frame
    void foo(){
        Debug.Log("onclick: add obstacles");

        if(flag) return;
        flag = true;

        int xMin = map.cellBounds.x;
        int xMax = map.cellBounds.x + map.cellBounds.size.x;
        int yMin = map.cellBounds.y;
        int yMax = map.cellBounds.y + map.cellBounds.size.y;

        for(int i = 0;i < num;i ++){
            GameObject instance = Instantiate(prefab[Random.Range(0,prefab.Length)], env.transform);
            instance.transform.localPosition = new Vector3(Random.Range(xMin,xMax), Random.Range(yMin,yMax),0);
        }
    }
}
